<template>
  <van-icon
    :color="value?'#3296fa':'#777'"
    :name="value?'star':'star-o'"
    @click="onCollect"
  />
</template>

<script>
import { addCollectArticle, deleteCollectArticle } from '@/api/article.js'
import { Toast } from 'vant'
export default {
  data () {
    return {

    }
  },
  props: {
    value: {
      type: Boolean,
      required: true
    },
    artId: {
      type: String,
      required: true
    }
  },
  created () {

  },

  methods: {
    async onCollect () {
      Toast.loading({
        duration: 0, // 持续展示 toast
        forbidClick: true,
        message: '加载....'
      })
      try {
        if (this.value) {
          // 代表已收藏，点击取消收藏
          await deleteCollectArticle(this.artId)
        } else {
          // 代表未收藏，点击收藏
          await addCollectArticle(this.artId)
        }
        this.$emit('input', !this.value)
        Toast.success('操作成功')
      } catch (err) {
        // 失败
        console.log(err)
        Toast.fail('操作失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
